package com.example.himalaya.presenters;

import com.example.himalaya.interfaces.IAlbumDetailPresenter;
import com.example.himalaya.interfaces.IAlbumDetailViewCallBack;
import com.example.himalaya.utils.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专辑详情页面的实现类
 * */
public class AlbumDetailPresenter  implements IAlbumDetailPresenter {
    private Album mTargetAlbum = null;
    private List<IAlbumDetailViewCallBack> mCallBacks = new ArrayList<>();
    private static  final String TAG = "AlbumDetailPresenter";

    private AlbumDetailPresenter(){}
    private static  AlbumDetailPresenter sInstance = null;
    public  static  AlbumDetailPresenter getInstance(){
        if (sInstance == null){
            synchronized (AlbumDetailPresenter.class){
                if (sInstance == null){
                    sInstance = new AlbumDetailPresenter();
                }
            }
        }
        return  sInstance;
    }

    @Override
    public void pull2RefreshMore() {

    }

    @Override
    public void loadMore() {
    }

    @Override
    public void getAlbumDetail(int albumId, int page) {
        //根据页码和专辑id
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.ALBUM_ID, albumId+"");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, page+"");
        map.put(DTransferConstants.PAGE_SIZE, Constants.COUNT_DETAIL+"");
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                if (trackList != null) {
                    List<Track> tracks = trackList.getTracks();
                    LogUtil.d(TAG,"tracks size --> "+tracks.size());
                    handlerAlbumDetailResult(tracks);
                }

            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG,"errorCode--> " + errorCode);
                LogUtil.d(TAG,"errorMsg--> " + errorMsg);
            }
            });
    }

    private void handlerAlbumDetailResult(List<Track> tracks) {
        for (IAlbumDetailViewCallBack mCallBack : mCallBacks){
            mCallBack.onDetailListLoaded(tracks);
        }
    }

    @Override
    public void registerViewCallBack(IAlbumDetailViewCallBack detailViewCallBack) {
        if (!mCallBacks.contains(detailViewCallBack)){
            mCallBacks.add(detailViewCallBack);
            if (mTargetAlbum != null) {
                detailViewCallBack.onAlbumLoaded(mTargetAlbum);
            }
        }
    }

    @Override
    public void unRegisterViewCallBack(IAlbumDetailViewCallBack detailViewCallBack) {
        mCallBacks.remove(detailViewCallBack);
    }

    public  void setTargetAlbum(Album targetAlbum){
        this.mTargetAlbum = targetAlbum;

    }
}
